<?php
header('Content-Type:text/xml;charset="utf-8');
require '../config.php';
require PATH . 'classes/ofj-orm/Evento.php';
require PATH . 'classes/ofj-orm/Noticia.php';

Lang::setSource('rss');
$lang = (LANG == 'es' ? '' : '_en');

// Obtener una lista de eventos y noticias mezclados y ordeanados por fecha
// MySQL Ninja mode activated!
$St = SahORMHandler::query(
  "SELECT
    `id`,
    'evento' AS `type`,
    `titulo{$lang}` AS `titulo`,
    `descripcion{$lang}` AS `contenido`,
    `fecha_creacion` FROM `evento`
  UNION SELECT
    `id`,
    'noticia' AS `type`,
    `titulo{$lang}` AS `titulo`,
    `contenido{$lang}` AS `contenido`,
    `fecha_creacion` FROM `noticia`
    WHERE `publicada`='S'
  ORDER BY `fecha_creacion` DESC LIMIT 50"
  , null, 'default');

echo '<?xml version="1.0" encoding="utf-8" ?>';
?>
<rss version="2.0">
<channel>

  <title><?php echo Lang::get('title') ?></title>
  <description><?php echo Lang::get('description') ?></description>
  <link><?php echo BASE ?></link>
  <image>
    <url><?php echo BASE, 'img/ofj-logo.jpg'?></url>
    <title><?php echo Lang::get('title') ?></title>
    <link><?php echo BASE ?></link>
    <description><?php echo Lang::get('title') ?></description>
    <width>204</width>
    <height>126</height>
  </image>

  <?php foreach($St->fetchAll(PDO::FETCH_OBJ) as $Item): ?>
  <item>
  <title><?php
    echo ($Item->type == 'evento' ? Lang::get('event') : Lang::get('notice'))
      , $Item->titulo
  ?></title>
  <description><![CDATA[<?php
  $desc_parts = explode('<!-- pagebreak -->', $Item->contenido);
  echo $desc_parts[0];
  ?>]]></description>
  <link><?php
    if($Item->type == 'evento'):
      echo BASE , 'conciertos-eventos/evento/?id=', $Item->id;
    else:
      echo BASE , 'noticias/?id=', $Item->id;
    endif;
  ?></link>
  </item>
  <?php endforeach; ?>

  </channel>
</rss>
